using B.S.Data.MES.API.Applications.BaseData.Command.CheJianCommand;
using B.S.Domain.BasicData.Factory_modeling.shop_management;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.ChejianCommandHandlers
{
    /// <summary>
    /// 获取所有车间信息命令处理器
    /// </summary>
    public class GetQueryChejianCommandHandler : IRequestHandler<GetQueryChejianCommand, APIRequst<List<shop_management>>>
    {
        private readonly IBaseRepository<shop_management> shopRepository;

        /// <summary>
        /// 构造函数注入仓储
        /// </summary>
        /// <param name="shopRepository">车间管理仓储</param>
        public GetQueryChejianCommandHandler(IBaseRepository<shop_management> shopRepository)
        {
            this.shopRepository = shopRepository;
        }

        /// <summary>
        /// 处理查询请求
        /// </summary>
        /// <param name="request">查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>车间信息列表</returns>
        public Task<APIRequst<List<shop_management>>> Handle(GetQueryChejianCommand request, CancellationToken cancellationToken)
        {
            // 获取所有未删除的车间信息
            var list = shopRepository.GetAll().Where(x => x.isDel == false).ToList();

            // 使用Task.FromResult包装返回结果，并确保状态码和消息一致
            return Task.FromResult(new APIRequst<List<shop_management>>
            {
                Data = list,
                Msg = RequstCode.成功.ToString(),
                Code = RequstCode.成功
            });
        }
    }
}